import * as ModelProto from '@/proto/model/ModelProto'
import ListenerInterface from '@/listener/ListenerInterface'
import {Command} from '@/const/Constants'
import { useSearchParams } from 'react-router-dom'
import websocketClient from '@/api/WebSocketClient'
class ConnectListener implements ListenerInterface {
  constructor() {}
  //执行连接成功处理
  apply = (response: ModelProto.Response) => {
    if (response.resp?.code == 200) {
      //连接成功，发送登录消息
      const [searchParams] = useSearchParams()
      //获取请求参数
      let cipherText = searchParams.get('cipherText')
      let secretKey = searchParams.get('secretKey')
      let request = ModelProto.Request.create()
      request.header = initHeader()
      request.auth = ModelProto.AuthPack.create()
      request.auth.cipherText = cipherText
      request.auth.secretKey = secretKey
      websocketClient.sendMessage(request)
    }
  }
}

const initHeader = (): ModelProto.Header => {
  let header = ModelProto.Header.create()
  header.command = Command.AUTH
  return header
}

export default ConnectListener
